#include "mywidget.h"
#include <QApplication>

#include "myplayer/SrDemux.h"
#include "myplayer/SrDecode.h"

#include "myplayer/SrResample.h"
#include "myplayer/SrAudioPlay.h"

#include "myplayer/SrAudioThread.h"
#include "myplayer/SrVideoThread.h"

#include "myplayer/SrDemuxThread.h"

#include <iostream>

#include <QThread>

class TestThread :public QThread
{
public:
    void Init()
    {
        demux.Open("e:\\test.mp4");

        //cout << "seek = " << demux.Seek(0.95) << endl;

        //std::cout << "vdecode.Open() = " << vdecode.Open(demux.CopyVPara(),true) << std::endl;


        audiothread.Open(demux.CopyAPara(), demux._sampleRate, demux._channels);
        audiothread.start();

//        videothread.Open(demux.CopyVPara(), video, demux._width, demux._height);
//        videothread.start();


    }
    void run()
    {
        for (;;)
        {
            AVPacket *pkt = demux.Read();
            if (!pkt)
            {
                qDebug("break................\n");
                break;
            }
            if (demux.IsAudio(pkt))
            {
                //adecode.Send(pkt);
                //AVFrame *frame = adecode.Recv();
                //cout << "Audio: " << frame << endl;

                audiothread.Push(pkt);
                //                <<" "
                //                av_packet_free(&pkt);
            }
            else
            {
                videothread.Push(pkt);
//                vdecode.Send(pkt);
//                AVFrame *frame = vdecode.Recv();
//                video->Repaint(frame);
                //msleep(40);
                //cout << "Video: " << frame << endl;
            }



        }
    }
public:
    SrDemux demux;
    //SrDecode vdecode;
    SrVideoWidget *video = NULL;

    SrAudioThread audiothread;
    SrVideoThread  videothread;

};

int main(int argc, char *argv[])
{

    //TestThread tt;




    //    SrDemux demux;
    //        demux.Open("e:\\test.mp4");

    //        /*AVCodecParameters *pa = demux.CopyVPara();
    //        avcodec_parameters_free(&pa);*/
    //        SrDecode vdecode;
    //        std::cout << "vdecode.Open() = " << vdecode.Open(demux.CopyVPara()) << std::endl;
    //        //vdecode.Clear();
    //        //vdecode.Close();
    //        SrDecode adecode;
    //        std::cout << "adecode.Open() = " << adecode.Open(demux.CopyAPara()) << std::endl;

    /*
        pa = demux.CopyAPara();
        avcodec_parameters_free(&pa);*/

    //        for (;;)
    //        {
    //            AVPacket *pkt = demux.Read();
    //            if (demux.IsAudio(pkt))
    //            {
    //                adecode.Send(pkt);
    //                AVFrame *frame = adecode.Recv();
    //                std::cout << "Audio: " << frame << std::endl;
    //            }
    //            else
    //            {
    //                vdecode.Send(pkt);
    //                AVFrame *frame = vdecode.Recv();
    //                std::cout << "Video: " << frame << std::endl;
    //            }
    //            if (!pkt)break;
    //        }



    QApplication a(argc, argv);
    MyWidget w;


    //w.ui->video;


    w.show();
    //w.ui->video->Init(tt.demux._width, tt.demux._height);

//    tt.video =  w.ui->video;
//    tt.Init();

//    tt.start();

//            SrDemuxThread dt;
//                dt.Open("e:\\test.mp4", w.ui->video);
//                dt.Start();


    return a.exec();
}
